package com.weixing.mall.provider.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.MsgCodeLimit;
import com.weixing.mall.provider.model.dto.MsgCodeLimitChgWhiteListDto;
import com.weixing.mall.provider.model.dto.MsgCodeLimitDto;
import com.weixing.mall.provider.model.query.MsgCodeLimitQuery;
import com.weixing.mall.provider.model.vo.MsgCodeLimitVo;
import com.weixing.mall.provider.service.IMsgCodeLimitService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 登录锁设置Api
 *
 * @author  gjq
 * @date  2019/9/20
 * @since  1.0
 **/
@RestController
@RequestMapping("msgCodeLimit")
public class MsgCodeLimitController extends BaseController {

    @Autowired
    private IMsgCodeLimitService msgCodeLimitService;

    /**
     * 加载登录锁设置列表
     *
     * @param query 查询条件
     * @return 列表
     */
    @GetMapping("")
    public Result<IPage<MsgCodeLimitVo>> list(MsgCodeLimitQuery query) {
        return ResultUtil.success(msgCodeLimitService.selectOfPage(initMpPage(), query));
    }

    /**
     * 保存
     * @param dto  请求参数
     * @return 结果
     */
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated MsgCodeLimitDto dto) {
        MsgCodeLimit msgCodeLimit = new MsgCodeLimit();
        BeanUtils.copyProperties(dto, msgCodeLimit);
        if (null == dto.getId()) {
            msgCodeLimitService.save(msgCodeLimit);
        } else {
            msgCodeLimitService.updateById(msgCodeLimit);
        }
        return ResultUtil.success();
    }

    /**
     * 发送次数清零
     * @param id      id
     * @return   结果
     */
    @PostMapping("reset")
    public Result reset(@PathVariable Long id) {
        MsgCodeLimit byId = msgCodeLimitService.getById(id);
        byId.setSendCount(0);
        msgCodeLimitService.updateById(byId);
        return ResultUtil.success();
    }

    /**
     * 修改白名单状态
     *
     * @param dto  参数
     * @return  结果
     */
    @PostMapping("updateWhiteList")
    public Result updateWhiteList(MsgCodeLimitChgWhiteListDto dto) {
        MsgCodeLimit byId = msgCodeLimitService.getById(dto.getId());
        byId.setIsInWhiteList(dto.getIsInWhiteList().getValue());
        msgCodeLimitService.updateById(byId);
        return ResultUtil.success();
    }

    /**
     * 删除登录锁
     *
     * @param id id
     * @return   结果
     */
    @PostMapping("del/{id}")
    public Result del(@PathVariable long id) {
        return msgCodeLimitService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }


    /**
     * 批量删除登录锁
     *
     * @param ids   ids
     * @return  结果
     */
    @PostMapping("delBatch")
    public Result delBatch(@RequestBody long[] ids) {
        List<long[]> longs = Arrays.asList(ids);
        msgCodeLimitService.removeByIds(longs);
        return ResultUtil.success();
    }

}
